package com.cpe.config;

import javax.annotation.Resource;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;

import com.qiniu.data.QiniuEnv;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;

@Configuration
@PropertySource({ "classpath:qiniu.properties" })
public class QiniuConfig {

	@Resource
	private Environment environment;

	@Bean
	public QiniuEnv qiniuEnv() {
		QiniuEnv qiniuEnv = new QiniuEnv();
		String accesskey = environment.getProperty("qiniu.accesskey");
		String secretkey = environment.getProperty("qiniu.secretkey");
		String bucket = environment.getProperty("qiniu.bucket");
		String dir = environment.getProperty("qiniu.dir");
		String host = environment.getProperty("qiniu.host");
		long expire = environment.getProperty("qiniu.uptoken.expire", Long.class);
		qiniuEnv.setAccesskey(accesskey);
		qiniuEnv.setSecretkey(secretkey);
		qiniuEnv.setBucket(bucket);
		qiniuEnv.setDir(dir);
		qiniuEnv.setHost(host);
		qiniuEnv.setExpire(expire);
		qiniuEnv.setAuth(Auth.create(accesskey, secretkey));
		return qiniuEnv;
	}

	@Bean
	public UploadManager uploadManager() {
		return new UploadManager();
	}
}
